package src.com.neusoft.nep.controller;

import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.DialogManager;
import src.com.neusoft.nep.entity.User;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class PublicSupervisorLoginViewController {

    @FXML
    private Button btn_login;
    @FXML
    private TextField txt_userNumber;
    @FXML
    private TextField txt_userPassword;
    private Stage currentStage;
    public void setLoginViewStage(Stage stage) {
        this.currentStage = stage;
    }
    //用户登录
    @FXML
    public void login() throws IOException {
        Map<String,User> userMap = FileUtils.readMapObject("Feedbacker.txt");
        String phoneNumber = txt_userNumber.getText();
        String password = txt_userPassword.getText();

        if (phoneNumber.isEmpty() || password.isEmpty()) {
            DialogManager.showDialog("登录失败", "手机号和密码不能为空");
            return;
        }

        if (userMap.containsKey(phoneNumber)) {
            User user = userMap.get(phoneNumber);
            if (password.equals(user.getPassword())) {
                FXMLLoader fxmlLoader = new FXMLLoader();
                URL url = getClass().getResource("../view/PublicSupervisorView1.fxml");
                fxmlLoader.setLocation(url);
                Parent root = fxmlLoader.load();

                // 获取下一个控制器并传递手机号
                PublicSupervisorView1Controller publicSupervisorController = fxmlLoader.getController();
                publicSupervisorController.setPhoneNumber(phoneNumber);

                Scene scene = btn_login.getScene();
                Stage stage = (Stage) scene.getWindow();
                scene.setRoot(root);
                stage.show();
            } else {
                DialogManager.showDialog("登录失败", "用户不存在或密码错误");
            }
        } else {
            DialogManager.showDialog("错误", "登录过程中发生错误: ");
        }
    }

    //开启注册页面
    @FXML
    public void register() {
        Stage stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/PublicSupervisorRegisterView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        PublicSupervisorRegisterViewController controller = fxmlLoader.getController();
        controller.setLoginViewStage(stage);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.showAndWait();
    }

    @FXML
    private Button btn_back;
    public void back() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/FirstWebView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        Stage stage = (Stage) scene.getWindow();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
}
